package com.yungu.swift.api.route.v1.passenger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.assets.passenger.model.dto.PassengerInvoiceDto;
import com.yungu.swift.assets.passenger.model.dto.PassengerRechargeDto;
import com.yungu.swift.assets.passenger.model.param.ApiPassengerInvoiceParam;
import com.yungu.swift.assets.passenger.service.PassengerInvoiceService;
import com.yungu.swift.assets.passenger.service.PassengerWalletService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.api.ApiPageParam;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.vo.JsonOrderListVo;
import com.yungu.swift.order.model.vo.PayVo;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.order.service.PayService;
import com.yungu.swift.system.finance.model.dto.FinanceActualFlowDto;
import com.yungu.swift.system.finance.service.FinanceActualFlowService;
import com.yungu.swift.system.sys.model.vo.SysPassengerAccountFlowVo;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * Created by cuixiuyin on 2018/3/27.
 * 乘客钱包API
 */
@Controller
@RequestMapping(value = "/api/v1/passenger")
@Api(value = "【乘客】【钱包】乘客钱包接口", tags = "乘客钱包相关操作")
public class WalletApi extends BaseApi {
    @Reference
    private PassengerWalletService passengerWalletService;
    @Reference
    private OrderService orderService;
    @Reference
    private PayService payService;
    @Reference
    private FinanceActualFlowService financeActualFlowService;
    @Reference
    private PassengerInvoiceService passengerInvoiceService;
    @Reference
    private SysAccountPassengerService SysAccountPassengerService;


    @ApiOperation(value = "返回乘客钱包信息", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/getWallet"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Map<String, Object>> getWallet(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            HttpServletRequest request) {
        return passengerWalletService.getWallet(getUserUid(request));
    }

    @ApiOperation(value = "返回乘客充值面额", notes = "返回成功或失败消息", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/listDenominations"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Collection<PassengerRechargeDto>> listDenominations(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "areaCode", value = "行政区划代码", required = false) @RequestParam(required = false) String areaCode) {
        return passengerWalletService.listDenominations(areaCode);
    }


    @ApiOperation(value = "乘客余额支付", notes = "返回成功或失败", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/pay"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<Map<String, String>> pay(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderUuid", value = "订单id", required = true) @RequestParam String orderUuid,
            @ApiParam(name = "couponUuid", value = "优惠券id") @RequestParam(required = false) String couponUuid,
            HttpServletRequest request) {
        return payService.trade(new PayVo(OrderConstant.PAY_PURPOSE_CHARGE_FARE, CommonConstant.PASSENGER,
                OrderConstant.PAY_TYPE_BALANCE, getUserUid(request), orderUuid, couponUuid, null, appid,
                null, null));
    }


    @ApiOperation(value = "乘客账户明细", notes = "分页流水信息", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/account"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<PageList<SysPassengerAccountFlowVo>> account(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页", required = true) @RequestParam Integer nowPage,
            HttpServletRequest request) {
        ApiPageParam apiPageParam = new ApiPageParam();
        apiPageParam.setUserUuid(getUserUid(request));
        apiPageParam.setNowPage(nowPage);
        return SysAccountPassengerService.flowQueryPage(apiPageParam);
    }

    @ApiOperation(value = "按照行程开票", notes = "返回成功或失败", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/billingByRoute"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<JsonOrderListVo>> billingByRoute(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页", required = false) @RequestParam(required = false) Integer nowPage,
            HttpServletRequest request) {
        //获取已完成的订单
        ApiPageParam apiPageParam = new ApiPageParam();
        apiPageParam.setNowPage(nowPage);
        apiPageParam.setUserUuid(getUserUid(request));
        return orderService.queryBillOrder(apiPageParam);
    }

    @ApiOperation(value = "按照充值金额开票", notes = "返回成功或失败", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/billingByCharge"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<FinanceActualFlowDto>> billingByCharge(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页", required = false) @RequestParam(required = false) Integer nowPage,
            HttpServletRequest request) {
        ApiPageParam apiPageParam = new ApiPageParam();
        apiPageParam.setUserUuid(getUserUid(request));
        apiPageParam.setNowPage(nowPage);
        return financeActualFlowService.queryBillFlow(apiPageParam);
    }

    @ApiOperation(value = "开票", notes = "返回成功或失败")
    @RequestMapping(value = {"/token/wallet/billing"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<PassengerInvoiceDto> billing(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "orderIds", value = "选中的开票记录id,用逗号拼接", required = true) @RequestParam String orderIds,
            @ApiParam(name = "type", value = "发票类型", required = true) @RequestParam Integer type,
            @ApiParam(name = "header", value = "发票抬头", required = true) @RequestParam String header,
            @ApiParam(name = "money", value = "发票金额", required = true) @RequestParam Double money,
            @ApiParam(name = "content", value = "发票内容", required = true) @RequestParam String content,
            @ApiParam(name = "recipient", value = "收件人", required = true) @RequestParam String recipient,
            @ApiParam(name = "mobile", value = "联系电话", required = true) @RequestParam String mobile,
            @ApiParam(name = "area", value = "所选地区", required = true) @RequestParam String area,
            @ApiParam(name = "detailAddress", value = "详细地址", required = true) @RequestParam String detailAddress,
            @ApiParam(name = "payType", value = "支付方式（1.货到付款）", required = true) @RequestParam Integer payType,
            @ApiParam(name = "taxpayerNo", value = "纳税人识别码", required = true) @RequestParam String taxpayerNo,
            HttpServletRequest request) {

        if (!StringUtils.isEmpty(mobile) && !ValidateUtils.validateMobile(mobile)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "手机号码格式错误");
        }
        if (!StringUtils.isEmpty(taxpayerNo)) {
            if (taxpayerNo.length() < 15 || taxpayerNo.length() > 20) {
                return ResponseData.buildErrorResponse(10000, "纳税人识别码格式错误");
            }
            Pattern pattern = compile("[\\W]");
            Matcher matcher = pattern.matcher(taxpayerNo);
            if (matcher.find()) {
                return ResponseData.buildErrorResponse(10000, "纳税人识别码格式错误");
            }
        }
        ApiPassengerInvoiceParam passengerInvoiceDto = new ApiPassengerInvoiceParam();
        passengerInvoiceDto.setType(type);
        passengerInvoiceDto.setUserUuid(getUserUid(request));
        passengerInvoiceDto.setHeader(header);
        passengerInvoiceDto.setMoney(money);
        passengerInvoiceDto.setContent(content);
        passengerInvoiceDto.setRecipient(recipient);
        passengerInvoiceDto.setMobile(mobile);
        passengerInvoiceDto.setArea(area);
        passengerInvoiceDto.setDetailAddress(detailAddress);
        passengerInvoiceDto.setPayType(payType);
        passengerInvoiceDto.setOrderIds(orderIds);
        passengerInvoiceDto.setTaxpayerNo(taxpayerNo);
        return passengerInvoiceService.billing(passengerInvoiceDto);
    }

    @ApiOperation(value = "开票历史", notes = "返回成功或失败", response = ResponseData.class)
    @RequestMapping(value = {"/token/wallet/listBillingHistory"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public ResponseData<PageList<PassengerInvoiceDto>> listBillingHistory(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "nowPage", value = "当前页", required = true) @RequestParam(required = false) Integer nowPage,
            @ApiParam(name = "type", value = "开票类型", required = false) @RequestParam(required = false) Integer type,
            HttpServletRequest request) {
        Map<String, Object> map = MapUtils.build(4);
        map.put("passengerUuid", getUserUid(request));
        map.put("type", type);
        return passengerInvoiceService.queryPage(map, new ApiPageParam(nowPage));
    }
}
